
package com.example.fw.base;

import java.awt.image.BufferedImage;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.fw.Application;
import com.example.fw.base.Util.FileUtil;
import com.example.fw.base.Util.Zd;
import com.example.fw.main.s.BService;
import com.myjar.Stringutil;

@RestController
public class BaseController {
	 @Autowired
	protected BService mBService;
	

	/**
	 * 字典查询
	 */
	@RequestMapping(value = "/zidian", method = RequestMethod.POST)
	public  RequestType zdcx(String pack,String field,String key) throws Exception {
		try {
			pack=pack.toLowerCase();
			field=field.toLowerCase();
			pack = pack.replaceFirst(pack.substring(0, 1), pack.substring(0, 1).toUpperCase());
			Class<?> cl = Class.forName("com.bm.Aenum."+pack+"_"+field);
			Method m = cl.getMethod("values");
			Object[] obj = (Object[]) m.invoke(cl);
			List<Zd> listZd = new ArrayList<>();
			if(Stringutil.isBlank(key)){
				for (Object object : obj) {
					Zd mzd = new Zd();
					mzd.setKey(Integer.parseInt(object.getClass().getMethod("getKey").invoke(object).toString()));
					mzd.setValue(object.getClass().getMethod("getValue").invoke(object).toString());
					listZd.add(mzd);
				}
				return sendTrueData(listZd);
			}else{
				for (Object object : obj) {
					if(object.getClass().getMethod("getKey").invoke(object).toString().equals(key)){
						Zd mzd= new Zd();
						mzd.setKey(Integer.parseInt(key));
						mzd.setValue(object.getClass().getMethod("getValue").invoke(object).toString());
						return sendTrueData(mzd);
					}
					
				}
			}
		} catch (Exception e) {
			
			if(pack.contains("/")){
				String[] strs = pack.split("/");
				String str=null;
				for (String string : strs) {
					if(!Stringutil.isBlank(string)){
						if("sp".equals(string)){
							string="xsp";
						}
						str=string;
						break;
					}
				}
				if(!Stringutil.isBlank(str)){
					if(str.equals("user"))str="jiabiuser";
					Sql msql = new Sql();
					pack="select COLUMN_NAME 字段名,COMMENTS 注释 from user_col_comments where Table_Name ='"+str.toUpperCase()+"'";
					if(!Stringutil.isBlank(field)){
						pack=pack+" and column_name='"+field.toUpperCase()+"'";
					}
					msql.setSql(pack);
					//这里根据实际放
				return	sendTrueData(mBService.exeSelectSql(msql));
				}
			}
		}
		
		
		return sendTrueMsg("未找到对应值");
	}

	@RequestMapping(value ="/uploadFile", method = RequestMethod.POST)
	public RequestType uploadFile(HttpServletRequest req) throws Exception {
		return sendTrueData(FileUtil.doFileUploadOne(req));
	}
	


	/**
	 * 成功，只返回提示信息
	 * */
	protected RequestType sendTrueMsg(String msg){
		return getRequestType(null, msg, null);
	}
	/**
	 * 成功，只返回数据
	 * */
	protected RequestType sendTrueData(Object obj){
		return getRequestType(null, null, obj);
	}
	/**
	 * 失败，返回提示信息
	 * */
	protected RequestType sendFalse(String msg){
		return getRequestType(-1, msg, null);
	}
	/**
	 * 失败，返回提示信息，带着stat
	 * */
	protected RequestType sendFalse(String msg,int status){
		return getRequestType(status, msg, null);
	}

	private RequestType getRequestType(Integer code,String msg,Object data){
		RequestType reqt = new RequestType();
		//状态码
		if(code!=null)reqt.setStatus(code);
		else reqt.setStatus(200);
		//提示信息
		if(msg!=null)reqt.setMessage(msg);
		//数据
		reqt.setData(data);
		//时间
		reqt.setTimestamp(System.currentTimeMillis());
		//运行时间，测试使用
//		reqt.setRuntime(System.currentTimeMillis()-InterceptorConfig.time);
		Application.out(reqt);
		return reqt;		
	}

/**
 * 输出一张图片
 * */    
    public void sendImage( HttpServletResponse response,BufferedImage image) throws Exception {
        ServletOutputStream out = response.getOutputStream();  
        ImageIO.write(image, "jpg", out);
	      out.flush();  
	      out.close();
        
    }



}
